// miniprogram/pages/checkProject/checkProject.js
var app = getApp();
//const host = "https://www.city-tech360.com/";
const host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    creator: false,
    worker: false,
    noOne: false,
    projectId: "",
    imgSrc:"",
    projectInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var projectId = options.projectId;
    var that = this;
    that.setData({
      projectId: projectId,
    })
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    //TODO 在此处检查项目是否是自己的
    wx.request({
      url: host + 'city360/project/query_if_mine_or_participated',
      method: 'POST',
      data:{
        openid: app.globalData.openid,
        projectId: that.data.projectId,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      complete: res=>{
        console.log("查询是否是自己的项目", res);
        if(res.data === "wrong"){
          console.log("出错了 项目不存在");
          wx.showModal({
            title: '出错了',
            content: '项目不存在',
            showCancel: false,
            success: res => {
              wx.navigateBack();
            }
          });
          return;
        }else{
          if (res.data === "creator") {
            that.setData({
              creator: true
            });
          } else if (res.data === "worker") {
            that.setData({
              worker: true
            });
          } else if (res.data === "no one") {
            that.setData({
              noOne: true
            });
          }
          //在此处获得项目信息
          get_project_info(that, projectId);
        }
        
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  reCreate: function(e){
    wx.navigateBack({
    });
    wx.redirectTo({
      url: '../newProject/newProject?projectId=' + this.data.projectId,
    });
  },

  joinProject: function(e){
    var that = this;
    wx.showLoading({
      title: '加入中',
      mask: true
    });
    wx.request({
      url:  host + 'city360/joinProject',
      method: "POST",
      data: {
        openid: app.globalData.openid,
        projectId: that.data.projectId,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      complete: res=>{
        console.log("加入项目完毕", res);
        wx.hideLoading();
        that.setData({
          noOne: false,
          worker: true,
        });
      }
    })
  },

  changeStatus: function(e){
    var that = this;
    wx.navigateTo({
      url: './changeStatus/changeStatus?projectStatus=' + that.data.projectInfo.projectStatus
      + "&projectId=" + that.data.projectId,
    })
  },

  deleteProject: function(e){
    var that = this;
    wx.showLoading({
      title: '删除中',
      mask: true,
    })
    wx.request({
      url: host + 'city360/project/deleteProject',
      method: "POST",
      data:{
        projectId: that.data.projectId,
        openid: app.globalData.openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      complete: function(res){
        console.log("删除完", res);
        if(res.statusCode === 200){
          console.log("成功接收返回");
          if(res.data === 'projectId wrong'){
            console.log("project不存在，请调试");
          }
          wx.hideLoading();
          wx.navigateBack();
        }
      }
    })
  },

  exitProject: function(e){
    var that = this;
    wx.showLoading({
      title: '删除中',
      mask: true,
    })
    wx.request({
      url:  host + 'city360/project/exitProject',
      method: "POST",
      data: {
        projectId: that.data.projectId,
        openid: app.globalData.openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      complete: function (res) {
        console.log("退出完", res);
        if (res.statusCode === 200) {
          console.log("成功接收返回");
          if (res.data === 'projectId wrong') {
            console.log("project不存在，请调试");
          }
          wx.hideLoading();
          wx.navigateBack();
        }else{
          wx.showModal({
            title: '出错了',
            content: '发生未知错误',
            showCancel: false
          });
          wx.hideLoading();
        }
      }
    })
  }
})



function get_project_info(page, projectId){
  var that = page;
  return wx.request({
    url: host + 'city360/project/getProject',
    method: "POST",
    data: {
      projectId: page.data.projectId,
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    complete: res=>{
      console.log("下载项目信息完毕", res);
      wx.hideLoading();
      if(res.statusCode === 200){
        console.log("下载成功");
        var imageFileName = res.data.imageFileName
        that.setData({
          projectInfo: res.data,
          projectId: res.data.projectId,
          imgSrc: host + imageFileName + ".jpg",
        })
        /*
        wx.downloadFile({
          url: "https://www.city-tech360.com/city360/downloadImg/" + imageFileName,
          success: res2 => {
            console.log(res2);
            if (res2.statusCode === 200) {
              var img_path = res2.tempFilePath;
              console.log("下载图片完毕");
              console.log(res2);
              that.setData({
                projectInfo: res.data,
                projectId: res.data.projectId,
                imgSrc: img_path,
              });
              wx.hideLoading();
            } else {
              wx.showModal({
                title: '出错了',
                content: '请稍候再试',
                showCancel: false,
              });
            }
          }
        })*/

        /*Promise.all([do_download_img(page, res.data.imageFileName)]).then(res2=>{
          console.log("下载图片完毕");
          console.log(res2);
          page.setData({
            projectInfo: res.data,
            projectId: res.data.projectId,
            imgSrc: get_img_path(res.data.imageFileName),
          });
        });*/
      }
    }
  })
}

function do_download_img(page, imageFileName) {
  var downloadTask = wx.downloadFile({
    url: host + "city360/downloadImg/" + imageFileName,
    filePath: wx.env.USER_DATA_PATH + '/' + imageFileName + '.jpg',
  });
  return downloadTask;
}

function get_img_path(imageFileName) {
  return wx.env.USER_DATA_PATH + '/' + imageFileName + '.jpg';
}